/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tuoc;

import java.sql.ResultSet;
import java.util.Map;
import java.util.Vector;

/**
 *
 * @author apple
 */
public class tables_model extends My_model{
    int id;
    String name;
    int order_by;
    
    
    public Vector get_elements()
    {
        ResultSet rs = this.executeSqlString("SELECT * FROM tables");
        return this.to_domain_models(rs);
    }
    
    public tables_model get_element_by_id(String id)
    {
        ResultSet rs = this.executeSqlString("SELECT * FROM tables WHERE id = " + id);
        return this.to_domain_model(rs);
    }
    
    public boolean insert_data(Map<String,String> data)
    {
        String sqlString = String.format("INSERT INTO tables VALUES(NULL,'%s',%s)",
                (data.get("name") != null) ? data.get("name") : "NULL",
                (data.get("order_by") != null) ? data.get("order_by") : "NULL"
                );
        int result = this.insertDatabase(sqlString);
        return (result != -1) ? true : false;
    }
    
    public tables_model to_domain_model(ResultSet rs)
    {
        tables_model result = new tables_model();
        try {
            if(rs.next())
            {
                result.id = rs.getInt("id");
                result.name = rs.getString("name");
                result.order_by = rs.getInt("order_by");
            }    
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return result;
    }
    
    public Vector to_domain_models(ResultSet rs)
    {
        Vector result = new Vector();
        try {
            while(rs.next())
            {
                tables_model item = new tables_model();
                item.id = rs.getInt("id");
                item.name = rs.getString("name");
                item.order_by = rs.getInt("order_by");
                
                result.add(item);
            }
            
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return result;
    }
}
